﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page
{  
    ///////////////////////////////////////////////////////////////////////////////////////////// 
    #region "Page"
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Init(object sender, EventArgs e)
    {
        //TODO 添加必要的页面初始化内容
    }

    #endregion

    ///////////////////////////////////////////////////////////////////////////////////////////// 
    #region "JS"
    public void JavascriptAttach()
    {
        //按情况添加必要的JS
    }
    #endregion
     
    ///////////////////////////////////////////////////////////////////////////////////////////// 
    #region "Connection" 
    public string CurrentConnectionString
    {
        get
        {
            if ((Session[WorkFlowCommon.CST.SESSION_CLIENT_CONNSTRING] == null))
            {
                HttpCookie myCookie = HttpContext.Current.Request.Cookies[WorkFlowCommon.CST.COOKIE_USER_SETTINGS];
                if (myCookie != null )
                {
                    Session[WorkFlowCommon.CST.SESSION_CLIENT_CONNSTRING] = new WorkFlowCommon.Crypt().Decrypt(myCookie[WorkFlowCommon.CST.SESSION_CLIENT_CONNSTRING]);
                }
            }
            if (((Session[WorkFlowCommon.CST.SESSION_CLIENT_CONNSTRING] == null) || (Session[WorkFlowCommon.CST.SESSION_CLIENT_CONNSTRING] == String.Empty)))
            {
                try
                { 
                    HttpContext.Current.Response.Redirect((("~/Connect.aspx?dec=1&discsrc=" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"])
                                    + (HttpContext.Current.Request.ServerVariables["URL"] + "&xxx=connstringpropery")));
                }
                catch (Exception ex)
                {
                }
            }
            return Session[WorkFlowCommon.CST.SESSION_CLIENT_CONNSTRING].ToString();
        }
        set
        {
            // session
            Session[WorkFlowCommon.CST.SESSION_CLIENT_CONNSTRING] = value;
            // cookie
            HttpCookie myCookie = HttpContext.Current.Request.Cookies[WorkFlowCommon.CST.COOKIE_USER_SETTINGS];
            if (myCookie != null )
            {
                myCookie[WorkFlowCommon.CST.SESSION_CLIENT_CONNSTRING] = new WorkFlowCommon.Crypt().Encrypt(value);
                myCookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(myCookie);
            }
        }
    }

    public int CurrentLogin
    {
        get
        {
            if ((Session[WorkFlowCommon.CST.SESSION_LOGIN_ID_GLOBAL] == null))
            {
                HttpCookie myCookie = HttpContext.Current.Request.Cookies[WorkFlowCommon.CST.SESSION_LOGIN_ID_GLOBAL];
                if (myCookie != null )
                {
                    Session[WorkFlowCommon.CST.SESSION_LOGIN_ID_GLOBAL] = myCookie.Value;
                }
            }
            return (int)Session[WorkFlowCommon.CST.SESSION_LOGIN_ID_GLOBAL];
        }
        set
        {
            Session[WorkFlowCommon.CST.SESSION_LOGIN_ID_GLOBAL] = value;
            HttpCookie myCookie = new HttpCookie(WorkFlowCommon.CST.SESSION_LOGIN_ID_GLOBAL);
            myCookie.Value = value.ToString();
            myCookie.Expires = DateTime.Now.AddDays(7);
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }
    }

    public WorkFlowData.WorkFlowDataContext  CurrentClientBase
    {
        get
        {
            WorkFlowData.WorkFlowDataContext wf = new WorkFlowData.WorkFlowDataContext(this.CurrentConnectionString);
            return wf;
        }
        set
        {
            Session[WorkFlowCommon.CST.SESSION_DB] = value; 
        }
    }
    #endregion 
}